package test19.writer_;

import java.io.FileWriter;
import java.io.IOException;

public class FileWriter_ {
    public static void main(String[] args) {
        String filePath = "d:\\note.txt";
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(filePath);//默认是覆盖写入
            //fileWriter = new FileWriter(filePath,true);追加
            // write(int):写入单个字符
            fileWriter.write('H');
            //write(char[]):写入指定数组
            char[] chars = {'a','b','c'};
            fileWriter.write(chars);
            //write(char[],off,len):写入指定数组的指定部分
            fileWriter.write("~~~~教育",0,3);
            //write（string）：写入整个字符串
            fileWriter.write("你好，北京");
            //write(string,off,len):写入字符串的指定部分
            fileWriter.write("上海天津",0,2);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //对应FileWriter , 一定要关闭流，或者flush才能真正的把数据写入到文件
            try {
                //fileWriter.flush();
                //关闭文件流，等价 flush() + 关闭
                fileWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        System.out.println("写入成功");
    }
}